package com.yida.gwj.config.service.impl;

import com.yida.gwj.common.constant.GwjConstants;
import com.yida.gwj.config.mapper.GwjAttachmentStatusMapper;
import com.yida.gwj.config.service.GwjAttachmentStatusService;
import com.yida.gwj.config.util.RedisConstants;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @Description:
 * @Author: PPV
 * @Date: 2023-04-24
 */
@Service
public class GwjAttachmentStatusServiceImpl implements GwjAttachmentStatusService {

    @Resource
    private GwjAttachmentStatusMapper statusMapper;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public int banUp(String serverIdentification, String outServerIdentification) {
        cleanCache(serverIdentification, outServerIdentification);
        return statusMapper.update(handleParams(serverIdentification, outServerIdentification, Integer.valueOf(GwjConstants.NO)));
    }

    @Override
    public int allowUp(String serverIdentification, String outServerIdentification) {
        cleanCache(serverIdentification, outServerIdentification);
        return statusMapper.update(handleParams(serverIdentification, outServerIdentification, Integer.valueOf(GwjConstants.YES)));
    }

    /**
     * 参数处理
     *
     * @param serverIdentification
     * @param outServerIdentification
     * @param allowUpFlag
     * @return
     */
    private HashMap<String, Object> handleParams(String serverIdentification, String outServerIdentification, Integer allowUpFlag) {
        return new HashMap<String, Object>(3) {{
            put("serverIdentification", serverIdentification);
            put("outServerIdentification", outServerIdentification);
            put("allowUpFlag", allowUpFlag);
        }};
    }

    /**
     * 清除缓存
     *
     * @param serverIdentification
     * @param outServerIdentification
     */
    private void cleanCache(String serverIdentification, String outServerIdentification) {
        String key = RedisConstants.ATTACH_STATUS_KEY + outServerIdentification + serverIdentification;
        redisTemplate.delete(key);
    }

}
